package com.lingyun.ucm.common.bootstrap;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

/**
 * 
 * @author Terrence2013
 * 2018年1月24日 上午9:48:50
 * 加载本地配置
 */
public class Bootstrap {
	private static Bootstrap instance = null;
	private static Object lock = new Object();
	private InitConfig cache = null;
	
	public static Bootstrap inst(){
		if(null == instance){
			synchronized(lock){
				if(null == instance){
					instance = new Bootstrap();
				}
			}
		}
		return instance;
	}

    private Bootstrap() {
    	InputStream in = null;
        try {
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream("bootstrap.properties");
            Properties properties = new Properties();
            properties.load(in);
            cache = new InitConfig();
            cache.setZookeeper(properties.getProperty(InitConfig.KEY_ZOOKEEPER));
            cache.setClientInitConfigNames(properties.getProperty(InitConfig.KEY_CLIENT_CONFIG_NAMES));
            cache.setServer(properties.getProperty(InitConfig.KEY_SERVER));
        } catch (Exception e) {
            //异常不处理
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                //异常不处理
            }
        }
    }
    
    public InitConfig getCache() {
		return cache;
	}

	public class InitConfig implements Serializable{
		/**
		 * 
		 */
		private static final long serialVersionUID = -1045129540510445825L;
		
		public final static String KEY_ZOOKEEPER = "zookeeper";
		public final static String KEY_CLIENT_CONFIG_NAMES = "client.names";
		public final static String KEY_SERVER = "server";
    	
		/**
		 * Zookeeper的IP和端口字符串集合(xxxx:xx,xxxx:xx,xxxx:xx)
		 */
		private String zookeeper;
		/**
		 * 客户端读取的系统配置名称(xxxx,xxxx)
		 */
		private String clientInitConfigNames;
		/**
		 * 服务端的IP
		 */
		private String server;
		
		public String getZookeeper() {
			return zookeeper;
		}
		public void setZookeeper(String zookeeper) {
			this.zookeeper = zookeeper;
		}
		public String getClientInitConfigNames() {
			return clientInitConfigNames;
		}
		public void setClientInitConfigNames(String clientInitConfigNames) {
			this.clientInitConfigNames = clientInitConfigNames;
		}
		public String getServer() {
			return server;
		}
		public void setServer(String server) {
			this.server = server;
		}
    }

}
